from Model.basicModel import Model
from Model.charModelUtil import util
import os
class charModel(Model):
    """
    部分属性继承自父类basicModel
    __strategy 表示计算CDList和ICDList时的分组策略号，目前只实现了一个策略，分成6组，只能填1
    __ICDList 表示根据目前训练集算出的理想字符分布，list
    __CDList 表示目前给定要预测的样本集中最后一个预测样本的字符分布
    __positiveRate 表示目前要预测的样本集中所有的样本（按序）被预测为正类的可能性，用于画ROC
    """
    __strategy=None
    __ICDList=None
    __CDList=None
    __positiveRate=None

    def __init__(self,trainingSet=None):
        super().__init__(trainingSet)
        self.__strategy=1
        self.__ICDList=[]
        self.__CDList=[]
        self.__positiveRate=[]

    def train(self,userStrategy=None):
        "训练函数，要求训练集不为空"
        if self.trainingSet!=None:
            self.__ICDList=util.dealdata(self.trainingSet,strategyNum=self.__strategy,userStrategy=userStrategy)
        else:
            print("缺少训练集...程序终止...")
            os._exit(0)

    def predict(self,samples,realRst,threshold=0.0001,userStrategy=None):
        """
        samples:list
        realRst:list
        threshold:float
        samples、realRst两个参数应该是同样的长度
        threshold 表示被判为正类的最小值
        userStrategy 是一个函数名，该函数给出了用户自定义的分组策略，
        """
        if self.__ICDList!=None:
            predictRst=[]
            self.__CDList=[]
            self.__positiveRate=[]
            super().setSampleToPredict(samples)
            super().setPredictResult(realRst)
            for sample in samples:
                stringDistribute=util.getDistribute(sample,strategyNum=self.__strategy,userStrategy=userStrategy)
                self.__CDList.append(stringDistribute)
                temp=util.chi2Rst(self.__ICDList, stringDistribute)
                self.__positiveRate.append(tuple(temp)[1])
            predictRst=util.makeDecesion(threshold, self.__positiveRate)
            super().setPredictResult(predictRst)
            return predictRst
        else:
            print("未进行训练...程序终止...")
            os._exit(0)

    def getStrategy(self):
        return self.__strategy

    def setStrategy(self,strategy):
        if strategy!=None:
            self.__strategy=strategy

    def getICDList(self):
        return self.__ICDList

    def setICDList(self,icdlist):
        if icdlist!=None and isinstance(icdlist,list):
            self.__ICDList=icdlist

    def getCDList(self):
        return self.__CDList

    def setCDList(self,cdlist):
        if cdlist!=None and isinstance(cdlist,list):
            self.__CDList=cdlist

    def getPositiveRate(self):
        return self.__positiveRate

    def setPositiveRate(self,positiveRate):
        if positiveRate!=None and isinstance(positiveRate,list):
            self.__positiveRate=positiveRate

if __name__ == '__main__':
    c=charModel()
    c.devTest()
